<?php include("function.php"); ?>
<?php
header("Content-Type: text/html;charset=utf-8");

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "data";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
$conn->query("SET NAMES UTF8");
function new_post()
{
    global $conn;
    $getdate = date("Y-m-d h:i:s");
    switch ($_POST['type']) {
        case '0':
            $sql = "INSERT INTO mainpost(`userid`,`topic`,`content`,`posttime`) VALUES ({$_POST['userid']},'{$_POST['topic']}','{$_POST['content']}','{$getdate}');";
            break;
        case '1':
            $sql = "INSERT INTO post(`userid`,`mainpostid`,`content`,`posttime`) VALUES ({$_POST['userid']},'{$_POST['mainpostid']}','{$_POST['content']}','{$getdate}');";
            break;
        default:
            # code...
            break;
    }
    // var_dump($sql);
    $user_result = $conn->query($sql);
    // var_dump($user_result);

    return;
}
if (isset($_POST["auto"]) && $_POST["auto"] == true) {
    new_post();
} elseif (isset($_POST["type"])) {
    if ($_POST["content"] == "") {
        echo ("<script>alert('发表失败')</script>");
        header('Location: postpc.php');
    }
    new_post();
    echo ("<script>alert('发表成功')</script>");
    switch ($_POST['type']) {
        case '0':
            header('Location: postpc.php');
            break;
        case '1':
            header('Location: detail.php?id=' . $_POST["mainpostid"]);
            break;
        default:
            # code...
            break;
    }
}
?>
<html>

<body>
    <form method="POST">
        <?php
        if ($_GET["type"] == 0) {
        ?>
            标题: <input type="text" name="topic" value="">
            <br><br>
        <?php
        }
        ?>
        <br><br>
        内容: <textarea name="content" rows="5" cols="40"></textarea>
        <br><br>
        <br><br>
        <input type="hidden" name="type" value="<?php echo ($_GET["type"]); ?>">
        <input type="hidden" name="userid" value="<?php echo (get_session_user()); ?>">
        <?php
                                                    if ($_GET["type"] == 1) {
        ?>
            <input type="hidden" name="mainpostid" value="<?php echo ($_GET["mainid"]); ?>">
        <?php
                                                        }
        ?>
        <input type="submit" name="submit" value="提交">
    </form>
</body>

</html>